﻿@{
    ViewBag.Title = "Danh sách kế hoạch giao hàng";
    var url = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
}
<script>
    app.controller('DeliveryPlanController', function ($scope, $compile, $http, service) {
        $scope.load = function (page) {
            if (page === 0 || (page > $scope.pageCount && $scope.pageCount > 0))
                return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            startloading($('#content'));

            var url = '@url' + '@Url.Action("DeliveryPlanApi", "Api")';
            $http.get(url, {
                params: {
                    pageNumber: page,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    deliveryPlanCode: $scope.deliveryPlanCode,
                    fullName: $scope.fullName,
                    datefrom: '',
                    dateto: ''
                }
            })
                .success(function (data) {
                    service.setCommonData($scope, data, data.DeliveryPlans);
                    stoploading($('#content'));
                })
                .error(function (data) {
                    stoploading($('#content'));
                    service.showModal($scope, $compile, 'error', data.InnerException.InnerException);
                });
        };

        $scope.setSelected = function (id) {
            service.setSelected($scope, id);
        };

        $scope.check = function (id) {
            $scope.checkedObj[id] = !$scope.checkedObj[id];
        };

        $scope.setSelectedAll = function () {
            service.setSelectedAll($scope);
        };

        $scope.refresh = function (keyCode) {
            if (keyCode == undefined || keyCode === 13)
                $scope.load($scope.pageNumber);
        };

        $scope.clearrefresh = function () {
            $scope.deliveryPlanCode = '';
            $scope.fullName = '';
            $scope.load($scope.pageNumber);
        };

        $scope.add = function () {
            window.location = '@Url.Action("Detail", "DeliveryPlan")';
        };

        $scope.edit = function (id) {
            if (id) {
                window.location = '@Url.Action("Detail", "DeliveryPlan")/' + id;
            } else {
                var selectedObj = service.getLastSelected($scope, $compile, service);
                if (selectedObj != '')
                    window.location = '@Url.Action("Detail", "DeliveryPlan")/' + selectedObj;
            }
        };

        $scope.confirmDelete = function () {
            service.showModal($scope, $compile, 'confirm', 'Bạn có chắc chắn muốn xóa dữ liệu?', 'delete()');
        };

        $scope.delete = function () {
            var deleteObj = service.getSelected($scope);
            if (deleteObj === '') return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            startloading($('#content'));

            var url = '@url' + '@Url.Action("DeliveryPlanApi", "Api")';
            $http.delete(url, {
                params: {
                    ids: deleteObj,
                    pageNumber: $scope.pageNumber,
                    orderBy: $scope.orderBy,
                    orderDirection: $scope.orderDirection,
                    deliveryPlanCode: $scope.deliveryPlanCode,
                    fullName: $scope.fullName,
                    datefrom: '',
                    dateto: ''
                }
            }).success(function (data) {
                service.setCommonData($scope, data, data.DeliveryPlans);
                stoploading($('#content'));
                service.showModal($scope, $compile, 'notify', 'Dữ liệu đã được xóa thành công!!!');
            }).error(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'error', data);
            });
        };

        $scope.dieuxe = function () {
            var deleteObj = service.getSelected($scope);
            if (deleteObj === '') return;

            $scope.checkedList = [];
            $scope.checkedObj = [{}];
            startloading($('#content'));

            var url = '@url' + '@Url.Action("DeliveryPlanApi", "Api")';
            $http.head(url, {
                params: {
                    ids: deleteObj
                }
            }).success(function (data) {
                stoploading($('#content'));
                service.showModal($scope, $compile, 'notify', 'Điều xe thành công!!!');
                $scope.load($scope.pageNumber);
            }).error(function (data) {
                stoploading($('#content'));

            });
        };

        $scope.sort = function (ob) {
            service.setSortingValue($scope, ob);
            $scope.load($scope.pageNumber);
        };

        $scope.statuslist = [{}];
        $scope.statuslist['N'] = 'Mới';
        $scope.statuslist['I'] = 'Đã Điều Xe';
        $scope.statuslist['C'] = 'Hoàn thành';

        $scope.types = [{}];
        $scope.types['SO'] = 'Giao hàng';
        $scope.types['IN'] = 'Nhận tại kho';

        $scope.orderBy = 'DeliveryPlanCode';
        $scope.orderDirection = 'descending';
        $scope.deliveryPlanCode = '';
        $scope.fullName = '';
        $scope.checkedList = [];
        $scope.checkedObj = [{}];
        $scope.load(1);
    });
</script>
<div ng-controller="DeliveryPlanController">
    @Html.Partial("_PageHeader")
    @Html.Partial("_ListViewHeader")

    <div id="content" class="content row">
        <div class="panel-body">
            <div id="search">
                <div class="row" style="margin-right: 0px">
                    <div class="col-lg-2">
                        <div class="form-group">
                            <label style="width: 110px">
                                Số kế hoạch
                            </label>
                            <input ng-model="deliveryPlanCode" ng-keypress="refresh($event.keyCode)" type="search" class="form-control" />
                        </div>
                    </div>
                    <div class="col-lg-2">
                        <div class="form-group">
                            <label style="width: 110px">
                                Tài xế
                            </label>
                            <input ng-model="fullName" ng-keypress="refresh($event.keyCode)" type="search" class="form-control" />
                        </div>
                    </div>
                    <div class="col-lg-2" style="clear: both">
                        @Html.Partial("Control/_Control_Datepicker", new Dictionary<string, string>
                    {
                        {"Text", "Từ ngày"},
                        {"Model", "product.AssignedDate"},
                        {"Name", "assignedDate"}
                    })
                    </div>
                    <div class="col-lg-2">
                        @Html.Partial("Control/_Control_Datepicker", new Dictionary<string, string>
                    {
                        {"Text", "đến"},
                        {"Model", "product.AssignedDate"},
                        {"Name", "assignedDate"}
                    })
                    </div>

                    @Html.Partial("_Search")
                </div>
            </div>
            <div class="table-responsive">
                @Html.Partial("_Toolbar_PagedList")
                <table id="data" class="table table-striped table-bordered table-hover">
                    <colgroup>
                        <col style="width: 50px" />
                        <col style="width: 100px" />
                        <col />
                        <col style="width: 110px" />
                        <col style="width: 110px" />
                        <col style="width: 110px" />
                        <col style="width: 200px" />
                        <col style="width: 100px" />
                    </colgroup>
                    <thead>
                        <tr>
                            <th>
                                <div>
                                    <input ng-model="selectedAll" ng-click="setSelectedAll()" style="margin-left: 10px;" class="center" type="checkbox">
                                </div>
                            </th>
                            <th ng-click="sort('DeliveryPlanCode')">MS K.H
                                <div ng-class="orderBy != 'DeliveryPlanCode' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('CreatedName')">N.Viên K.H
                                <div ng-class="orderBy != 'CreatedName' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('CreatedDate')">Ngày K.H
                                <div ng-class="orderBy != 'CreatedDate' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('Date')">Ngày giao
                                <div ng-class="orderBy != 'Date' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('DeliveryType')">Hình Thức
                                <div ng-class="orderBy != 'DeliveryType' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('FullName')">Tài xế
                                <div ng-class="orderBy != 'FullName' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')" class="sorting fa">
                                </div>
                            </th>
                            <th ng-click="sort('Status')" class="center">T.Thái
                                <div ng-class="orderBy != 'Status' ? 'fa-sort': (orderDirection == 'ascending' ? 'fa-sort-asc' : 'fa-sort-desc')"
                                    class="sorting fa">
                                </div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-hide="totalItemOnPage == 0" ng-repeat="item in items" ng-click="setSelected(item.DeliveryPlanID)"
                            ng-dblclick="edit(item.DeliveryPlanID)">
                            <td class="center">
                                <div>
                                    <input ng-click="check(item.DeliveryPlanID)" ng-model="checkedObj[item.DeliveryPlanID]" class="center" type="checkbox" />
                                </div>
                            </td>
                            <td>
                                @{
                                    var detailurl = Url.Action("Detail", "DeliveryPlan", new { id = "{{id=item.DeliveryPlanID}}" });
                                    detailurl = HttpUtility.UrlDecode(detailurl);
                                }
                                <a data-ng-href="@detailurl">{{item.DeliveryPlanCode}}</a>
                            </td>
                            <td>{{item.UpdatedName}}</td>
                            <td class="center">{{item.UpdatedDate | date:'dd/MM/y'}}</td>
                            <td class="center">{{item.Date | date:'dd/MM/y'}}</td>
                            <td>{{types[item.DeliveryType]}}</td>
                            <td>{{item.FullName}}</td>
                            <td>{{statuslist[item.Status]}}</td>
                        </tr>
                        <tr ng-show="totalItemOnPage == 0">
                            <td colspan="100">Không có dữ liệu
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
